﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using nwrole.lib;

namespace nwrole.lib2 {
    public class StorageUtil {
        #region Plugins

        public static IDictionary ServiceDataToDictionary(ServiceData serviceData) {
            var dict = new Dictionary<string, object>
            {
                { "id", serviceData.Id.ToString("N") },
                { "name", serviceData.Name },
                { "display-name", serviceData.DisplayName },
                { "enabled", serviceData.Enabled },
                { "username", serviceData.Username },
                { "password", serviceData.Password },
                { "management-port", serviceData.ManagementPort },
                { "deployed", serviceData.Deployed },
                { "deployed-date", serviceData.DeployedDate },
                { "service-state", serviceData.ServiceState },
                //{ "plugins", new List<object>() }
            };

            //WriteLog("ServiceDataToDictionary: " + serviceData.PluginDatas.Count);

            /*foreach (PluginData pluginData in serviceData.PluginDatas)
                ((List<object>)dict["plugins"]).Add(PluginDataToDictionary(pluginData));*/

            return dict;
        }

        public static ServiceData ServiceDataFromDictionary(IDictionary dict) {
            ServiceData serviceData = new ServiceData {
                Id = new Guid(dict["id"] as string),
                Name = (string)dict["name"],
                DisplayName = (string)dict["display-name"],
                Enabled = (bool)dict["enabled"],
                Username = (string)dict["username"],
                Password = (string)dict["password"],
                ManagementPort = (int)dict["management-port"],
                Deployed = (bool)dict["deployed"],
                DeployedDate = (DateTime?)dict["deployed-date"],
                ServiceState = (ServiceState)Enum.Parse(typeof(ServiceState), (string)dict["service-state"], true),
                //PluginDatas = new List<PluginData>(),
            };

            /*foreach (object obj in (IEnumerable)dict["plugins"])
                serviceData.PluginDatas.Add(PluginDataFromDictionary((IDictionary) obj));*/
            
            return serviceData;
        }

        public static IDictionary PluginDataToDictionary(PluginData pluginData) {
            return new Dictionary<string, object>
            {
                //{ "id", pluginData.Id.ToString("N") },
                { "enabled", pluginData.Enabled },
                { "ClassName", pluginData.ClassName }
                /*{ "name", pluginData.Name },
                { "description", pluginData.Description },
                { "author", pluginData.Author },
                { "assembly", pluginData.Assembly },
                { "created", pluginData.Created },
                { "updated", pluginData.Updated },
                { "added", pluginData.Added }*/
            };
        }

        public static PluginData PluginDataFromDictionary(IDictionary dict) {
            return new PluginData {
                //Id = new Guid(dict["id"] as string),
                Enabled = (bool)dict["enabled"],
                ClassName = dict["ClassName"] as string,
                /*Name = dict["name"] as string,
                Description = dict["description"] as string,
                Author = dict["author"] as string,
                Assembly = dict["assembly"] as string,
                Created = (DateTime)dict["created"],
                Updated = (DateTime)dict["updated"],
                Added = (DateTime)dict["added"]*/
            };
        }

        public static void StorePlugin(PluginBundle bundle) {
            string pluginDirectory = Path.Combine(Configuration.PluginsDirectory, bundle.Id.ToString("N"));
            if (Directory.Exists(pluginDirectory))
                Directory.Delete(pluginDirectory, true);
            Directory.CreateDirectory(pluginDirectory);

            foreach (string file in bundle.Files)
                File.Copy(file, Path.Combine(pluginDirectory, Path.GetFileName(file)));
        }

        public static void DeletePlugin(Guid id) {
            string pluginDirectory = Path.Combine(Configuration.PluginsDirectory, id.ToString("N"));
            if (Directory.Exists(pluginDirectory))
                Directory.Delete(pluginDirectory, true);
        }

        private static void WriteLog(string content) {
            DateTime now = DateTime.Now;
            var path = "c:/log/nwrole/PluginService/";
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);

            System.IO.File.AppendAllText(path + "log " + now.ToString("yyyy-MM-dd") + ".txt", now.ToString("yyyy-MM-dd hh:mm:ss ") + content + "\r\n");
        }

        #endregion
    }
}
